package com.gt.login;

import java.io.File;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class DomContentAppend {
	String inputFileName;
	String outputFileName;

	public DomContentAppend(String inputFileName, String outputFileName) {
		this.inputFileName = inputFileName;
		this.outputFileName = outputFileName;
	}

	public boolean editUser(User user) {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document doc;// whole doc
		try {
			String fil=new File(inputFileName).toURI().toString();
			System.out.println(fil);
			doc = factory.newDocumentBuilder().parse(fil);
//			doc = factory.newDocumentBuilder().parse(new InputSource(new ByteArrayInputStream(inputFileName.getBytes("utf-8"))));
			Node documentElem = doc.getDocumentElement();
			// documentElem=root node
			NodeList nodeList = documentElem.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				Node childNode = nodeList.item(i);
				System.out.println(childNode.getNodeName());
				if (childNode.getNodeName().equalsIgnoreCase("user")) {
					// userNode got
					NodeList level2 = childNode.getChildNodes();
					for (int j = 0; j < level2.getLength(); j++) {
						Node child = level2.item(j);
						System.out.println(child.getNodeName());
						// search for userName node
						if ("username".equalsIgnoreCase(child.getNodeName())
								&& user.getUserName().equalsIgnoreCase(
										child.getTextContent().trim())) {
							changeNodeContent(child, doc, user);
							saveToFile(doc);
							return true;
						}
					}
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			return false;
		}
		return false;
	}

	private void changeNodeContent(Node child, Document doc, User user) {
		Node oldNode = child.getParentNode();
		Element newNode = doc.createElement("user");
		// change name
		Element nameNode = doc.createElement("username");
		newNode.appendChild(nameNode);
		Text userName = doc.createTextNode(user.getUserName());
		nameNode.appendChild(userName);
		// TODO: change others
		//password
		Element passN = doc.createElement("password");
		newNode.appendChild(passN);
		Text passT = doc.createTextNode(user.getPassword());
		passN.appendChild(passT);
		//lastname
		Element lnameN = doc.createElement("lname");
		newNode.appendChild(lnameN);
		Text lnameT = doc.createTextNode(user.getLastName());
		lnameN.appendChild(lnameT);
		//firstName
		Element fnameN = doc.createElement("fname");
		newNode.appendChild(fnameN);
		Text fnameT = doc.createTextNode(user.getFirstName());
		fnameN.appendChild(fnameT);
		
		//firstName
		Element addrN = doc.createElement("address");
		newNode.appendChild(addrN);
		Text addrT = doc.createTextNode(user.getAddress());
		addrN.appendChild(addrT);
		
		//phone
		Element phoneN = doc.createElement("phone");
		newNode.appendChild(phoneN);
		Text phoneT = doc.createTextNode(user.getPhoneNo());
		phoneN.appendChild(phoneT);
		
		
		
		Element root = doc.getDocumentElement();
		root.replaceChild(newNode, oldNode);
	}

	private void saveToFile(Document doc) {
		// save
		// set up a transformer
		TransformerFactory transfac = TransformerFactory.newInstance();
		Transformer trans;
		try {
			trans = transfac.newTransformer();

			// create string from xml tree
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			DOMSource source = new DOMSource(doc);
			trans.transform(source, result);
			FileUtils.saveStringToFile(sw.toString(), outputFileName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
